package luongvd.logoutaccount.common;

import java.util.List;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;

public class Utils {

	public static boolean appInstalledOrNot(String packagename, Context mContext) {
		PackageManager pm = mContext.getPackageManager();
		boolean app_installed = false;
		try {
			pm.getPackageInfo(packagename, PackageManager.GET_ACTIVITIES);
			app_installed = true;
		} catch (PackageManager.NameNotFoundException e) {
			app_installed = false;
		}
		return app_installed;
	}

	public static String getAccountLoginGoogleFirst(Context mContext) {
		String accountName = "";

		AccountManager manager = AccountManager.get(mContext);
		Account[] accounts = manager.getAccounts();
		for (Account account : accounts) {
			String name = account.name;
			String type = account.type;
			if (type.equals("com.google")) {
				accountName = name;
				return accountName;
			}
		}
		return accountName;
	}

	public static String getGooglePlayStoreUrl(String namePackage,
			Context mContext) {
		PackageManager packageManager = mContext.getApplicationContext()
				.getPackageManager();
		Uri marketUri = Uri.parse("market://details?id=" + namePackage);
		Intent marketIntent = new Intent(Intent.ACTION_VIEW).setData(marketUri);
		if (marketIntent.resolveActivity(packageManager) != null) {
			return "market://details?id=" + namePackage;
		} else
			return "https://play.google.com/store/apps/details?id="
					+ namePackage;
	}

	public static void killGooglePlay(Context mContext) {
		String packageName = "com.android.vending";
		ActivityManager mActivityManager = (ActivityManager) mContext
				.getSystemService(Context.ACTIVITY_SERVICE);
		List<ActivityManager.RunningAppProcessInfo> listOfProcesses = mActivityManager
				.getRunningAppProcesses();
		for (ActivityManager.RunningAppProcessInfo process : listOfProcesses) {
			// if (process.processName.contains(packageName)) {
			// int pid = process.pid;
			// Intent i =
			// mContext.getPackageManager().getLaunchIntentForPackage(
			// process.processName);
			// i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			// // i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			// mContext.startActivity(i);
			// ((Activity) mContext).finish();

			// mActivityManager.restartPackage(process.processName);
			// android.os.Process.killProcess(pid);
			// mActivityManager.killBackgroundProcesses(packageName);
			// System.exit(0);

			Intent homeIntent = new Intent(Intent.ACTION_MAIN);
			homeIntent.addCategory(Intent.CATEGORY_HOME);
			homeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			homeIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			mContext.startActivity(homeIntent);
			((Activity) mContext).finish();

			// System.runFinalizersOnExit(true);

		}
	}
	
	public static boolean isPackageInstalled(String packagename, Context context) {
	    PackageManager pm = context.getPackageManager();
	    try {
	        pm.getPackageInfo(packagename, PackageManager.GET_ACTIVITIES);
	        return true;
	    } catch (NameNotFoundException e) {
	        return false;
	    }
	}
	
	
}
